package com.hw.core.system.service;

import com.hw.core.system.entity.CourseReview;
import com.hw.core.system.repository.CourseReviewRepository;
import com.hw.core.system.repository.CourseReviewRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(readOnly = true)//全部方法只读事务
@CacheConfig(cacheNames = "courseReview")
public class CourseReviewService {
    @Autowired
    private CourseReviewRepository courseReviewRepository;


    @Cacheable(key = "'list'")//缓存返回值，下次调用方法体不执行
    public List<CourseReview> findAllCourseReview() {
        return courseReviewRepository.findAll();
    }

    @Cacheable(key = "#id")
    public CourseReview findCourseReviewById(Integer id) {
        return courseReviewRepository.findOne( id );
    }




    @CacheEvict(allEntries = true)
    @Transactional
    public CourseReview add(CourseReview courseReview) {
        return courseReviewRepository.save( courseReview );
    }

    @CacheEvict(allEntries = true)
    @Transactional
    public void delete(Integer id) {
        courseReviewRepository.delete( id );
    }
}
